package com.hyzh.latte.sand.socket;

import com.coffee.socketio.SocketioInitialize;
import com.coffee.socketio.SocketioServer;
import com.coffee.socketio.command.CommandEnum;
import com.coffee.socketio.handler.HandlerParameter;
import com.coffee.socketio.handler.SocketioCloseHandler;
import com.coffee.socketio.handler.SocketioConnectHandler;
import com.coffee.socketio.handler.SocketioUserValidHandler;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author songkui
 * @since 2024/4/24 11:13
 */
@Slf4j
public class SanderHandlerBuilder {
    @Getter
    private final static HandlerParameter handlerParameter = new HandlerParameter();

    public static SanderHandlerBuilder builder() {
        return new SanderHandlerBuilder();
    }


    public SanderHandlerBuilder socketConnectHandler(SocketioConnectHandler connectHandler) {
        handlerParameter.setSocketConnectHandler(connectHandler);
        return this;
    }

    public SanderHandlerBuilder socketCloseHandler(SocketioCloseHandler closeHandler) {
        handlerParameter.setSocketCloseHandler(closeHandler);
        return this;
    }

    public SanderHandlerBuilder socketUserValidHandler(SocketioUserValidHandler userValidHandler) {
        handlerParameter.setSocketUserValidHandler(userValidHandler);
        return this;
    }

    public SanderHandlerBuilder initHandler(Map<Integer, CommandEnum> commandEnumMap, String handlerPackagePath) throws Exception {
        SocketioInitialize.initializeCommandModel(commandEnumMap);
        SocketioInitialize.initializeHandlerMapping(handlerPackagePath, commandEnumMap);
        return this;
    }

    public void start(Integer port, String host) {
        SocketioServer socketServer = new SocketioServer();
        socketServer.start(port, host, handlerParameter);
    }

    private SanderHandlerBuilder() {
    }

}
